

import org.junit.After
import org.junit.Before
import org.junit.Test
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.Select
import org.openqa.selenium.support.ui.WebDriverWait

import static org.junit.Assert.assertNotSame

class ManipulationExerciseTest {

    static WebDriver driver
    static WebElement form

    @Before
    void createDriver() {
        driver = new FirefoxDriver()
        driver.navigate().to("http://compendiumdev.co.uk/selenium/basic_html_form.html")
        form = driver.findElement(By.cssSelector("input[type='submit']"))
    }

    @Test
    void shouldContainCorrectTitleWhenSubmittingTheForm() {
        def titleBeforeSubmission = driver.getTitle()
        form.submit()
        def titleAfterSubmission = driver.getTitle()
        assertNotSame("Titles are different", titleBeforeSubmission, titleAfterSubmission)
    }

    @Test
    void shouldPersistCommentsWhenSubmittingTheForm() {
        final def textAreaElement = driver.findElement(By.cssSelector("textarea[name='comments']"))
        final def comments = "some comments"
        textAreaElement.clear()
        textAreaElement.sendKeys(comments)
        submitForm()
        final renderedCommentsLocator = By.cssSelector("li[id='_valuecomments']")
        new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElement(renderedCommentsLocator, comments))
    }

    @Test
    void shouldPersistRadioBoxTwoStateWhenSubmittingTheForm() {
        final def radioTwoElement = driver.findElement(By.cssSelector("input[value='rd2']"))
        radioTwoElement.click()
        submitForm()
        final renderedRadioLocator = By.cssSelector("li[id='_valueradioval']")
        new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElement(renderedRadioLocator, "rd2"))
    }

    @Test
    void shouldPersistCheckBoxOneStateWhenSubmittingTheForm() {
        final def checkBoxElements = driver.findElements(By.cssSelector("input[name='checkboxes[]']"))
        checkBoxElements.each { element ->
            if (element.getAttribute("value") == "cb1") {
                element.click()
            } else if (element.isSelected()) {
                element.click()
            }
        }
        submitForm()
        final renderedChkBoxValLocator = By.cssSelector("li[id='_valuecheckboxes0']")
        final wait = new WebDriverWait(driver, 10)
        wait.until(ExpectedConditions.textToBePresentInElement(renderedChkBoxValLocator, "cb1")).
                and(wait.until(ExpectedConditions.invisibilityOfElementWithText(renderedChkBoxValLocator, "cb2")).
                        and(wait.until(ExpectedConditions.invisibilityOfElementWithText(renderedChkBoxValLocator, "cb3"))))
    }

    @Test
    void shouldPersistDropDownItemFiveWhenSubmittingTheForm() {
        final def optionFiveElement = driver.findElement(By.cssSelector("option[value='dd5']"))
        optionFiveElement.click()
        submitForm()
        final renderedOptionLocator = By.cssSelector("li[id='_valuedropdown']")
        final wait = new WebDriverWait(driver, 10)
        wait.until(ExpectedConditions.textToBePresentInElement(renderedOptionLocator, "dd5")).
                and(wait.until(ExpectedConditions.invisibilityOfElementWithText(renderedOptionLocator, "dd3")))
    }

    @Test
    void shouldPersistMultipleSelectsWhenSubmittingTheForm() {
        def selectElement = driver.findElement(By.cssSelector("select[name='multipleselect[]']"))
        new Select(selectElement).deselectAll()
        new Select(selectElement).selectByIndex(0)
        new Select(selectElement).selectByIndex(1)
        new Select(selectElement).selectByIndex(2)

//        final def multipleSelectElement = driver.findElements(By.cssSelector("option[value*='ms']"))
//        multipleSelectElement.each { selection -> selection.click() }

        submitForm()
        final selectionOneLocator = By.cssSelector("li[id='_valuemultipleselect0']")
        final selectionTwoLocator = By.cssSelector("li[id='_valuemultipleselect1']")
        final selectionThreeLocator = By.cssSelector("li[id='_valuemultipleselect2']")
        final selectionFourLocator = By.cssSelector("li[id='_valuemultipleselect3']")
        final wait = new WebDriverWait(driver, 10)
        wait.until(ExpectedConditions.textToBePresentInElement(selectionOneLocator, "ms1")).
                and(wait.until(ExpectedConditions.textToBePresentInElement(selectionTwoLocator, "ms2"))).
                and(wait.until(ExpectedConditions.textToBePresentInElement(selectionThreeLocator, "ms3"))).
                and(wait.until(ExpectedConditions.invisibilityOfElementWithText(selectionFourLocator, "ms4")))
    }

    @Test
    void shouldPersistFileNameWhenSubmittingTheForm() {
        def testFile = new File(this.getClass().getResource("/somefile").toURI())
        final def fileNameElement = driver.findElement(By.cssSelector("input[name='filename']"))
        fileNameElement.sendKeys(testFile.getAbsolutePath())
        submitForm()
        final renderedFileNameLocator = By.cssSelector("li[id='_valuefilename']")
        final wait = new WebDriverWait(driver, 10)
        wait.until(ExpectedConditions.textToBePresentInElement(renderedFileNameLocator, "somefile"))
    }

    private void submitForm() {
        form.submit()
        new WebDriverWait(driver, 10).until(ExpectedConditions.titleIs("Processed Form Details"))
    }

    @After
    void quitDriver() {
        driver.quit()
    }

}

